<style lang="scss">
.imgs{
  width: 680rpx;
  padding:  20rpx 35rpx;
  flex-wrap: wrap;
}
.img{
  width: 200rpx;
  height: 200rpx;
  margin-right: 10px;
  margin-bottom: 10px;
  .add{
    border:1px solid #ebedf0;
    width: 196rpx;
    height: 196rpx;
    padding-top: 32px;
    box-sizing: border-box;
  }
}
</style>
<template>

  <view class="page-wrap">

    <view>
      <view class="imgs bg-white">
        <upload-image @done="upImgs" limit="9"></upload-image>
      </view>
      <layout-cell-group>
        <fun-field
            :value="title"
            label="名称"
            placeholder="请输入名称"
            @change="bindTitleInput">
        </fun-field>
          <fun-field
              :value="desc"
              label="简单介绍"
              placeholder="请输入概述"
              type="textarea"
              @change="bindDescInput">
        </fun-field>
      </layout-cell-group>

    </view>

    <view class="p-15">
      <button size="large" class="m-t-15 m-b-15" type="primary" @click="subFunc">提交</button>
    </view>
  </view>

</template>
<script>
import { pageMixin } from '@/mixins'
import Promisify from '@/common/Promisify'
import LayoutIcon from '@/components/layout-icon/layout-icon'
import LayoutCellGroup from '@/components/layout-cell-group/layout-cell-group'
import FunField from '@/components/fun-field/fun-field'
import UploadImage from '@/components/upload-image/upload-image'
import { upSceneInfo } from '@/api'
import { Exception } from '@/common/Exception'
import { backFunc, error, modal, toast } from '@/common/fun'
export default {
  components: { UploadImage, FunField, LayoutCellGroup, LayoutIcon },
  mixins: [pageMixin],
  data () {
    return {
      purchaser: {},
      order_list_id: null,
      rate: 0,
      lat: null,
      lng: null,
      desc: '',
      title: '',
      content: '',
      imgs_temp: [],
      imgs: []
    }
  },
  methods: {
    upImgs ({ imgs, ext }) {
      console.log(imgs)
      this.imgs = imgs
    },
    onChange (e) {
      this.rate = e.detail
    },
    del (e) {
      this.imgs_temp.splice(e, 1)
    },
    bindTitleInput (val) {
      this.title = val
    },
    bindDescInput (val) {
      this.desc = val
    },
    upImg () {
      var self = this
      var allow = 9 - self.imgs_temp.length
      wx.chooseImage({
        count: allow,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          // tempFilePath可以作为img标签的src属性显示图片
          self.imgs_temp = self.imgs_temp.concat(res.tempFilePaths)
        }
      })
    },

    subFunc () {
      var self = this

      if (!self.lat || !self.lng) {
        error('经纬度必填')
        return
      }
      if (!self.title) {
        error('标题必填')
        return
      }

      if (self.imgs.length < 1) {
        error('图片必传')
        return
      }

      wx.showModal({
        title: '信息确认',
        showCancel: true,
        confirmText: '立即上传',
        cancelText: '我再改改',
        content: JSON.stringify({ title: self.title, desc: self.desc, lat: self.lat, lng: self.lng, pic: self.imgs[0] }),
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.showLoading({ title: '数据提交中' })

            var postData = { action: 'post', title: self.title, desc: self.desc, lat: self.lat, lng: self.lng, pic: self.imgs[0], imgs: self.imgs.join('|') }
            upSceneInfo({ ...postData }, { tip: '上传中', mask: true }).then(res => {
              if (res.code === 1) {
                toast('发布成功')
                setTimeout(() => {
                  backFunc()
                }, 1000)
              } else {
                modal({ title: '操作失败', content: res.msg })
              }
            }).catch(err => {
              Exception.handle(err)
            })
          } else if (res.cancel) {
            error('用户点击取消', 'none')
          }
        }

      })
    },
    async _init_func () {

    }
  },

  onReady () {
    Promisify('getLocation', { type: 'wgs84' }).then(res => {
      console.log('当前位置的经度：' + res.longitude)
      console.log('当前位置的纬度：' + res.latitude)
      this.lat = res.latitude
      this.lng = res.longitude
    })
  },
  onLoad (options) {
    this.id = options.id
    this._init_func()
  }
}
</script>
